package com.tlkj.common.curd.controller.admin;

import com.tlkj.common.curd.entity.ApiMenu;
import com.tlkj.common.curd.entity.BaseEntity.Save;
import com.tlkj.common.curd.entity.BaseEntity.Update;
import com.tlkj.common.curd.model.AjaxResult;
import com.tlkj.common.curd.service.ApiMenuService;
import com.tlkj.common.curd.vo.MenuTree;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * API目录管理 - Controller
 * @author flyong86
 * @since 2024年6月19日
 */
@RequiredArgsConstructor
@RequestMapping("/admin/common/curd/menu")
@RestController
public class AdminApiMenuController {

    public final ApiMenuService apiMenuService;

    @GetMapping("/tree")
    public List<MenuTree> getTree() {
        return apiMenuService.getTrees();
    }

    @PostMapping("/save")
    public AjaxResult save(@RequestBody @Validated(value = Save.class) ApiMenu apiMenu) {
        apiMenuService.insert(apiMenu);
        return AjaxResult.success(apiMenu);
    }

    @PostMapping("/update")
    public AjaxResult update(@RequestBody @Validated(value = Update.class) ApiMenu apiMenu) {
        apiMenuService.update(apiMenu);
        return AjaxResult.success(apiMenu);
    }

    @GetMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable("id") String id) {
        try {
            apiMenuService.deleteById(id);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }
}
